package org.factory.api;

import java.lang.reflect.ParameterizedType;
import java.util.List;


import org.apache.log4j.Logger;
import org.dao.api.Dao;

public abstract class Factory<T> {
	private Logger logger = Logger.getLogger(Factory.class);
	
	@SuppressWarnings("unchecked")
	public T createNewIntance(){
		Class<?> clazz = (Class<?>)((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[0];
		try{
			T newInstance = (T) clazz.newInstance();
//			if(!getDao().persist(newInstance)){
//				return null;
//			}
			return newInstance;	
		}catch(Exception ex){
			logger.error(ex.getMessage());
		}
		return null;
	}
	
	public List<T> findAll(){
		return getDao().findAll();
	}
	
	public T findById(Integer pId){
		return getDao().findById(pId);
	}
	
	public void commit(){
		getDao().commit();
	}
	
	public void roolback(){
		getDao().roolback();
	}
	
	public boolean delete(T pEntity){
		return getDao().delete(pEntity);
	}
	
	public boolean save(T pEntity){
		return getDao().persist(pEntity);
	}
	
	public abstract Dao<T> getDao();
}
